/*****************************************************
** Author: 
** Date: 2019-12-07
*
*****************************************************/

#include "networkapi_curve25519.h"
#include "curve25519_test.h"
#include "comm.h"
#include <time.h>
#include <string.h>
#include <stdlib.h>


#define CURVE25519_KEY_LENGTH 32

/**
 * @brief
 * @param  None
 * @retval None
 */
void curve25519_run(void)
{
	unsigned char public[2][CURVE25519_KEY_LENGTH];
	unsigned char private[2][CURVE25519_KEY_LENGTH];
	unsigned char shared[2][CURVE25519_KEY_LENGTH];

#if 1
	memset(public, 0x0, sizeof(public));
	memset(private, 0x0, sizeof(private));
#else 
	int i, j;
	/* 0. private should be initilized with random value */
	srand(time(NULL));
	for (j = 0; j < 2; ++j) {
		for (i = 0; i < CURVE25519_KEY_LENGTH; ++i) {
			private[j][i] = (unsigned char)rand();
		}
		hexdump(private[j], sizeof(private[j]), "generate private[%d] key", j);
	}
#endif

	/* 1. generate public key, private was given */
	int ret = networkapi_scalarmult_secure_base(public[0], private[0]);
	tc_log("networkapi_scalarmult_secure_base ret %d", ret);
	hexdump(public[0], sizeof(public[0]), "public[0] key");
	hexdump(private[0], sizeof(private[0]), "private[0] key");

	ret = networkapi_scalarmult_secure_base(public[1], private[1]);
	tc_log("networkapi_scalarmult_secure_base ret %d", ret);
	hexdump(public[1], sizeof(public[1]), "public[1] key");
	hexdump(private[1], sizeof(private[1]), "private[1] key");

	/* 2. calc shared key */
	ret = networkapi_scalarmult_secure(shared[0], private[0], public[1]);
	tc_log("calc [%d] shared ret %d", 0, ret);
	hexdump(shared[0], sizeof(shared[0]), "shared[0] key");

	ret = networkapi_scalarmult_secure(shared[1], private[1], public[0]);
	tc_log("calc [%d] shared ret %d", 1, ret);
	hexdump(shared[1], sizeof(shared[1]), "shared[1] key");
}

/* End of file */



